package com.xyd.Servlet;

import com.xyd.Biz.Impl.MessageBizImpl;
import com.xyd.Biz.MessageBiz;
import com.xyd.Entity.Message;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SelMessageServlet")
public class SelMessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        if(op.equals("list")){
            list(req, resp);
        }else if(op.equals("PLlist")){
            PLlist(req, resp);
        }else if(op.equals("Alllist")){
            Alllist(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    public void list(HttpServletRequest req, HttpServletResponse resp){
        MessageBiz messageBiz = new MessageBizImpl();
        int pNumMsg = Integer.parseInt(req.getParameter("pagenum"));
        int maxNum =messageBiz.selectMsgBLcount("1");
        maxNum = maxNum%5==0?maxNum/5:maxNum/5+1;
        maxNum = maxNum<=1?1:maxNum;
        pNumMsg = pNumMsg<1?1:pNumMsg;
        pNumMsg = pNumMsg>maxNum?maxNum:pNumMsg;
        List<Message> list = messageBiz.selectMsgBL((pNumMsg-1)*5,"1");
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("pNumMsg",pNumMsg);
        JsonServlet.getJson(req,resp,map);
    }

    public void Alllist(HttpServletRequest req, HttpServletResponse resp){
        MessageBiz messageBiz = new MessageBizImpl();
        int pNumMsg = Integer.parseInt(req.getParameter("pagenum"));
        int maxNum =messageBiz.selectMsgBLcount("2");
        maxNum = maxNum%5==0?maxNum/5:maxNum/5+1;
        maxNum = maxNum<=1?1:maxNum;
        pNumMsg = pNumMsg<1?1:pNumMsg;
        pNumMsg = pNumMsg>maxNum?maxNum:pNumMsg;
        List<Message> list = messageBiz.selectMsgBL((pNumMsg-1)*5,"2");
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("pNumMsg",pNumMsg);
        JsonServlet.getJson(req,resp,map);
    }

    public void PLlist(HttpServletRequest req, HttpServletResponse resp){
        MessageBiz messageBiz = new MessageBizImpl();
        int pNumMsg = Integer.parseInt(req.getParameter("pagenum"));
        int commodity_id = Integer.parseInt(req.getParameter("commodity_id"));
        int maxNum =messageBiz.selectMsgPLcount(commodity_id);
        maxNum = maxNum%5==0?maxNum/5:maxNum/5+1;
        maxNum = maxNum<=1?1:maxNum;
        pNumMsg = pNumMsg<1?1:pNumMsg;
        pNumMsg = pNumMsg>maxNum?maxNum:pNumMsg;
        List<Message> list = messageBiz.selectMsgPL((pNumMsg-1)*5,commodity_id);
        Map<String,Object> map =new HashMap<>();
        map.put("list",list);
        map.put("Msg_N",pNumMsg);
        JsonServlet.getJson(req,resp,map);
    }

}
